package cn.ante.controller;

import cn.ante.domain.User;
import cn.ante.handle.UserBlockHandle;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope // 刷新配置使用
@RestController
public class UserController {
    @Value("${temp.notify}")
    private String config;
    @GetMapping("/user/{id}")
    @SentinelResource(value = "user",
            blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = UserBlockHandle.class,
            fallback = "getByIdfallback")
    public User getUserById(@PathVariable("id") Long id){
//        System.out.println(config);
        int i = 1 / 0;
        return new User(id,"zs","我是颤三");
    }

//    public User exceptionHandler(@PathVariable("id") Long id){
//        // 当使用最大得异常去接得时候，限流降级会直接熔断，而不走限流，当使用alibba得异常时才会走限流方法
//
//        return new User(-1L,"限流了","限流了");
//    }

    // 熔断降级，参数和返回值与源方法一致
    public static User getByIdfallback(@PathVariable Long id, Throwable te){
        return new User(id,"zs:"+id, "熔断托底了");
    }
}
